/*
 * @Description: 生成 vue form
 * @Author: DHL
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2023-01-08 21:21:40
 */

import { firstToUpperCase, firstToLowerCase } from '../../utils/codeGenUtils'

export function createForm(codeGenConfig: any) {
  const classNameFirstToUpperCase = firstToUpperCase(codeGenConfig.className)

  return `
<!--
* @Description: ${codeGenConfig.genName} - 表单
* @Author: ${codeGenConfig.genAuthor}
* @Date: ${codeGenConfig.createData}
* @LastEditors: ${codeGenConfig.genAuthor}
* @LastEditTime: ${codeGenConfig.createData}
-->
<template>
  <tw-dialog 
    v-model="visible" 
    :title="title" 
    :loading="loading" 
    width="600px" 
    height="300px" 
    @close="handleClose"
  >
    <el-form ref="dbFormRef" :model="dbForm" :rules="rules" label-width="90px" :disabled="action === 'view'">
      ${getForm(codeGenConfig.optionsJson.columns)}
    </el-form>

    <template #footer>
      <el-button @click="handleReset(true)">取 消</el-button>
      <el-button v-if="action === 'add'" type="warning" @click="handleSubmit(true)">
        提交并继续
      </el-button>
      <el-button v-if="action !== 'view'" type="primary" @click="handleSubmit(false)">
        提 交
      </el-button>
    </template>
  </tw-dialog>
</template>

<script src="./useForm.ts"></script>

<style scoped lang="scss"></style> 
`
}

/**
 * 获取表单
 * @param columns
 */
function getForm(columns: any) {
  let rtn: Array<string> = []

  columns.forEach((item: any) => {
    if (item.form && item.columnKey !== '1') {
      rtn.push(
        `
      <el-form-item label="${item.javaComment}" prop="${item.javaField}">
        ${getComponent(item)}
      </el-form-item>
    `
      )
    }
  })
  return rtn.join('')
}

/**
 * 查询表单组件
 * @param item
 * @returns
 */
function getComponent(item: any) {
  let rtn: Array<string> = []

  switch (item.componentType) {
    case 'input':
      rtn.push(
        `<el-input v-model="dbForm.${item.javaField}" clearable placeholder="请输入${item.javaComment}"></el-input>`
      )
      break
    case 'textarea':
      rtn.push(
        `<el-input v-model="dbForm.${item.javaField}" type="textarea" :rows="2" clearable placeholder="请输入${item.javaComment}"></el-input>`
      )
      break
    case 'select':
      rtn.push(
        `<el-select v-model="dbForm.${item.javaField}" clearable placeholder="请选择${item.javaComment}">
          <el-option
            v-for="item in ${item.javaField}Data"
            :key="\`${item.javaField}_\${item.value}\`"
            :label="item.label"
            :value="item.value"
          >
        </el-select>`
      )
      break
    case 'inputNumber':
      rtn.push(
        `<el-input-number v-model="dbForm.${item.javaField}" clearable placeholder="请输入${item.javaComment}"/>`
      )
      break
    case 'radio':
      rtn.push(
        `<el-radio v-model="dbForm.${item.javaField}" label="${item.javaComment}">${item.javaComment}</el-radio>`
      )
      break
    case 'checkbox':
      rtn.push(
        `<el-checkbox v-model="dbForm.${item.javaField}" label="${item.javaComment}"></el-checkbox>`
      )
      break
    case 'datePicker':
      rtn.push(
        `<el-date-picker v-model="dbForm.${item.javaField}" type="date" clearable value-format="YYYY-MM-DD" placeholder="请选择${item.javaComment}" ></el-date-picker>`
      )
      break
    case 'dateTimePicker':
      rtn.push(
        `<el-date-picker v-model="dbForm.${item.javaField}" type="datetime" clearable value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择${item.javaComment}" ></el-date-picker>`
      )
      break
    case 'timePicker':
      rtn.push(
        `<el-time-picker v-model="dbForm.${item.javaField}" clearable placeholder="请选择${item.javaComment}"></el-time-picker>`
      )
      break
    case 'timeSelect':
      rtn.push(
        `<el-time-select v-model="dbForm.${item.javaField}" start="08:30" step="00:15" end="18:30" clearable placeholder="请选择${item.javaComment}"></el-time-select>`
      )
      break
    case 'switch':
      rtn.push(
        `<el-switch v-model="dbForm.${item.javaField}" inline-prompt active-text="是" inactive-text="否" />`
      )
      break
    case 'upload':
      rtn.push(``)
      break
    case 'dataDic':
      rtn.push(
        `<el-select v-model="dbForm.${item.javaField}" clearable placeholder="请选择${item.javaComment}">
          <tw-dic dicKey="${item.dataDic}"></tw-dic>
        </el-select>`
      )
      break
    case 'money':
      rtn.push(
        `<tw-money v-model="dbForm.${item.javaField}" clearable placeholder="请输入${item.javaComment}"></tw-money>`
      )
      break
    default:
      break
  }
  return rtn.join('')
}
